<?
/*! \brief Represents an OpenBT master node
 * 
 */
class Master extends Node
{	
	public function __construct($addr=null)
	{
		parent::__construct($addr);
		
		$this->_type = MASTER;
		$this->_regtype = 'SLAVE';
		$this->_table = 'slaves';
	}
	
	public function getLoad()
	{
		if($this->_addr == HOST_NAME)
			return $this->getLocalLoad();
		else
			return $this->getRemoteLoad();
	}
	
	public function getLocalLoad()
	{		
		$load = -1;
		
		//Mean and standard deviation of the slaves loads
		$sql = 'SELECT AVG(`load`), STDDEV_SAMP(`load`) FROM `'.$this->_table.
			'` WHERE `load` <> -1';
		$this->_db->setFetchMode(Zend_Db::FETCH_NUM);
		$result = $this->_db->fetchAll($sql,2);
		
		//result is just the mean if there's only one
		if(sizeof($result) > 0)
		{
			if($result[0][1] == null)
				$load = $result[0][0];
			else
				$load = $result[0][0] / $result[0][1];
		}
		return $load;
	}
	
	public function getSlaveList()
	{
		return $this->getAddressList();
	}

	public function getSlaveLeastLoad()
	{
		return $this->getNodeLeastLoad();
	}
	
	public function setSlaveLoad($addr, $load)
	{
		return $this->setLoad($addr, $load);
	}
}